package cn.com.yang.datastruct.queue;

/**
 * @since 2023.07.22
 * 单项队列
 */
public class QueueTest1 {
    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue(3);
        myQueue.insert(12);
        myQueue.insert(120);
        myQueue.insert(128);
        while (!myQueue.isEmpty()){
            System.out.println(myQueue.remove());
        }
    }
}
class MyQueue{
    private Object[] eleData;
    private int maxSize;//数组的最大长度
    private int rear;//队尾指针
    private int front;//队头指针
    private int nElems;// 队列中的元素的个数
    public MyQueue(int size){
        this.maxSize=size;
        eleData=new Object[size];
        rear = -1;
        front = 0;
        nElems = 0;
    }
    //插入队列元素数据的功能
    public void insert(Object data){
        // 判断队列是否已经满了,满了，新数据项就不应该插入
        if (nElems==maxSize){
            System.out.println("队列已经满了，新的数据项不能再插入了");
        }else {
            if (rear+1==maxSize){
                rear =-1;
            }
            eleData[++rear] = data;
            nElems++;
        }
    }
    //取出队列队头的数据项
    public  Object peek(){
        if (nElems==0){
            return null;
        }
        return eleData[front];
    }
    // 出队，移除,front移除
    public Object remove(){
        Object removeObj = null;
        if (nElems !=0){
            removeObj =eleData[front];
            eleData[front]=null;
            nElems--;
            if (front+1==maxSize){
                front =0;
            }else {
                front++;
            }
        }
        return removeObj;
    }
    // 判断队列是不是空队列
    public boolean isEmpty(){
        return nElems==0;
    }
    // 判断队列是否是满队列
    public boolean isFull(){
        return nElems==maxSize;
    }
    //返回队列的大小
    public int getSize(){
        return nElems;
    }
}
